package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.Constant;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.MD5;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 */
@Controller
@RequestMapping("/mimacontroller")
public class MiMacontroller {

	private static final Logger logger = Logger.getLogger(MiMacontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	String yhm=Constant.username;
	String mima=Constant.mima;
	String miyao=Constant.miyao;
	//普通用户密码找回
	@RequestMapping("/ptzh")
	@ResponseBody
	   
	public String puzh(HttpServletRequest request,String sfzh,String grzh,String ylsjh,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String password ,String dxyz) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());

		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String Key="";
		String a="";
		String Timestamp=sdf.format(date);
		String jm=mima+Timestamp+miyao;
		// 接口编码
		System.out.println(jm);
		MD5 md5=new MD5();
		a=md5.getMD5(jm).toString();
		// 接口编码
				body.setPassWd(a);
				body.setTimestamp(Timestamp);
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName",yhm);
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		

		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ105");
		//接口参数
		reqMap.put("OpenKey",Key);
		b.setUserName(sfzh);
		b.setDXYZM(dxyz);
		b.setPASSWORD(password);
		b.setGRZH(grzh);
		b.setCategory("1");
		reqMap.put("BODY",b);
		String result=opk.getOpenKey(reqMap, session);
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				map.put("code", 0); 
				map.put("msg", null);
				map.put("data",resMap.get("body"));
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}
	//高级用户密码找回
		@RequestMapping("/gjzh")
		@ResponseBody
		
		public String gjzh(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword ) throws IOException{
			//读取配置文件路劲
			SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
			Map remap = new HashMap();
			BODY body=new BODY();
			String Key="";
			String interfaceUrl = ReadConfig.readValue();
			if(sessionIn==null){
				body.setPassWd("123456");
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		
				
		}else if(sessionIn.getKEY()==null){
			body.setPassWd("123456");
			remap.put("AUTHORITY", "YBGJJ100");
			// 接口参数
			remap.put("BODY", body);
			String json3 = JSONUtil.ToJsongString2(remap);
			// 请求
			String result2 = HttpRequest.doPost(interfaceUrl, json3);
			logger.info("接口返回内容:" + result2);
			Map retMap2 = JSONUtil.toMap(result2);
			Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
			List jx = (List) mx2.get("RETDATA");
			Map m = (Map) jx.get(0);
			Key +=m.get("OpenKey").toString();	
			}
			
			Map reqMap = new HashMap();
			BODY b=new BODY();
			OpenKey opk=new OpenKey();
			//接口编码
			reqMap.put("AUTHORITY", "YBGJJ105");
			//接口参数
			reqMap.put("OpenKey",Key);
			b.setIDCardNumber(sfzh);
			b.setKAHAO(grzh);
			b.setUserName(yhmc);
			b.setCategory(Category);
			reqMap.put("BODY",b);
			String result=opk.getOpenKey(reqMap, session);
			logger.info("接口返回内容:"+result);
			if(result != null){
				Map resMap = JSONUtil.toMap(result);
				if(resMap.get("RETURNCODE").equals("100")){
					
					map.put("code", 0); 
					map.put("msg", null);
					map.put("data", resMap.get("body"));
					list.add(resMap);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}else{
					map.put("code", 1); 
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}
			}
			return null;
		}
		//单位用户密码找回
				@RequestMapping("/dwzh")
				@ResponseBody
				
				public String dwzh(HttpServletRequest request,String jbrzjhm,String dwmc,String Category,HttpSession session, HttpServletResponse response,String dxyz,String ylsjh,String password,String dwzh ) throws IOException{
					SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());

					//读取配置文件路劲
					String interfaceUrl = ReadConfig.readValue();
					Map remap = new HashMap();
					BODY body = new BODY();
					Date date=new Date();
					SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
					String Key="";
					String a="";
					String Timestamp=sdf.format(date);
					String jm=mima+Timestamp+miyao;
					// 接口编码
					System.out.println(jm);
					MD5 md5=new MD5();
					a=md5.getMD5(jm).toString();
					// 接口编码
							body.setPassWd(a);
							body.setTimestamp(Timestamp);
							remap.put("AUTHORITY", "YBGJJ100");
							// 接口参数
							remap.put("LOGUserName",yhm);
							remap.put("BODY", body);
							String json3 = JSONUtil.ToJsongString2(remap);
							// 请求
							String result2 = HttpRequest.doPost(interfaceUrl, json3);
							logger.info("接口返回内容:" + result2);
							Map retMap2 = JSONUtil.toMap(result2);
							Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
							List jx = (List) mx2.get("RETDATA");
							Map m = (Map) jx.get(0);
							Key += m.get("OpenKey").toString();		

					Map reqMap = new HashMap();
					BODY b=new BODY();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ105");
					//接口参数
					reqMap.put("OpenKey",Key);
					b.setUserName(dwzh);
					b.setDXYZM(dxyz);
					b.setPASSWORD(password);
					b.setSJHM(ylsjh);
					b.setCategory("3");
					reqMap.put("BODY",b);
					String result=opk.getOpenKey(reqMap, session);
					logger.info("接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							map.put("code", 0); 
							map.put("msg", null);
							map.put("data",resMap.get("body"));
							list.add(resMap);
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}else{
							map.put("code", 1);
							map.put("CW", resMap.get("RETURNDESC").toString());
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}
					}
					return null;
				}
				//个人登录后里的修改密码
				@RequestMapping("/text")
				@ResponseBody
				
				public String gr(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword ) throws IOException{
					//读取配置文件路劲
					SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
					
					String interfaceUrl = ReadConfig.readValue();
					Map reqMap = new HashMap();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ155");
					//接口参数
					reqMap.put("OpenKey",sessionIn.getKEY());
					BODY b=new BODY();
					b.setUserName(sessionIn.getGRZH());
					b.setZJHM(sessionIn.getZJHM());
					b.setPASSWORD(xpassword);
					b.setCategory("1");
					reqMap.put("BODY",b);
					String result=opk.getOpenKey(reqMap, session);
					logger.info("接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							
							map.put("code", 0); 
							map.put("msg", "1");
							map.put("data",resMap.get("body"));
							list.add(resMap);
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}else{
							map.put("code", 1);
							map.put("msg", "1");
							map.put("CW", resMap.get("RETURNDESC").toString());
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
							return null;
						}
					}
					return null;
				}
				//单位登录后里的修改密码
				@RequestMapping("/dwxg")
				@ResponseBody
				public String dw(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword,String username,String password,String checkcode ) throws IOException{
					//读取配置文件路劲
					SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
			       
					String interfaceUrl = ReadConfig.readValue();
					
					Map reqMap = new HashMap();
					BODY body=new BODY();
					SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
					String Timestamp=sdf.format(new Date());
					String a=mima+Timestamp+miyao;
					MD5 md5=new MD5();
					a=md5.getMD5(a).toString();
					body.setPassWd(a);
					
					body.setTimestamp(Timestamp);
					reqMap.put("AUTHORITY", "YBGJJ100");
					reqMap.put("LOGUserName", yhm);
					// 接口参数
					reqMap.put("BODY", body);
					String json3 = JSONUtil.ToJsongString2(reqMap);
					// 请求
					String result2 = HttpRequest.doPost(interfaceUrl, json3);
					Map retMap2 = JSONUtil.toMap(result2);
					Map rem=(Map) retMap2.get("body");
					List rel=(List) rem.get("RETDATA");
					Map m=(Map) rel.get(0);
					OpenKey opk=new OpenKey();
					
					
					
					//验证单位
					
					reqMap.put("AUTHORITY", "YBGJJ124");
					//接口参数
					reqMap.put("OpenKey",m.get("OpenKey"));
					BODY b=new BODY();
					b.setDWZH(username);
					reqMap.put("BODY",b);
					String result1=opk.getOpenKey(reqMap, session);
					Map resMap1 = JSONUtil.toMap(result1);
					Map m1=(Map) resMap1.get("body");
					List ll= (List) m1.get("RETDATA");
					Map mm=(Map) ll.get(0);
					if(mm.get("DWMIYAO")==null)
						mm.put("DWMIYAO", "");
					String miyao=mm.get("DWMIYAO").toString();
					miyao=MD5.getMD5(miyao+logincontroller.logionstring(response, session));
					if(!miyao.toString().equals(checkcode.toString()))
						return null;
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ155");
					//接口参数
					reqMap.put("OpenKey",m.get("OpenKey"));
					b.setUserName(username);
					b.setPASSWORD(password);
					b.setCategory("3");
					reqMap.put("BODY",b);
					String result=opk.getOpenKey(reqMap, session);
					logger.info("接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							
							String sShow="changpasswordok";
							return sShow;
						}
					}
					String sShow="changpasswordok";
					return sShow;
				}
				//短信验证码
				@RequestMapping("/dxyzm")
				@ResponseBody
				
				public String dxyzm(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String ylsjh,String jpassword,String qpassword ) throws IOException{
					//读取配置文件路劲
					SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
			       
					String interfaceUrl = ReadConfig.readValue();
					Map remap = new HashMap();
					BODY body = new BODY();
					Date date=new Date();
					SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
					String Key="";
					if(sessionIn!=null){
						if(!(sessionIn.getKEY().equals("")|| sessionIn.getKEY()==null)){
							 Key=sessionIn.getKEY();
						}
					}
					String a="";
					String Timestamp=sdf.format(date);
					String jm="123456"+Timestamp+"y1uUuBiikf0a5FHZ";
					// 接口编码
					System.out.println(jm);
					MD5 md5=new MD5();
					a=md5.getMD5(jm).toString();
					if(sessionIn==null){
						body.setPassWd(a);
							remap.put("AUTHORITY", "YBGJJ100");
							// 接口参数
							remap.put("BODY", body);
							body.setTimestamp(Timestamp);
							String json3 = JSONUtil.ToJsongString2(remap);
							// 请求
							String result2 = HttpRequest.doPost(interfaceUrl, json3);
							logger.info("接口返回内容:" + result2);
							Map retMap2 = JSONUtil.toMap(result2);
							try {
								if(retMap2.get("RETURNCODE").equals("100")){
									Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
									List jx = (List) mx2.get("RETDATA");
									Map m = (Map) jx.get(0);
									Key += m.get("OpenKey").toString();		
								}else{
									map.put("code", 1);
									map.put("CW", retMap2.get("RETURNDESC").toString());
									list.add(retMap2);
									response.setHeader("Content-type", "text/html;charset=UTF-8");
									response.setCharacterEncoding("UTF-8");
									String jsonString = JSONUtil.ToJsongString2(map);
									PrintWriter pw = response.getWriter();
									pw.write(jsonString);
									return null;
								}
							} catch (Exception e) {
								map.put("code", 1);
								map.put("CW", "登录失败！");
								list.add(retMap2);
								response.setHeader("Content-type", "text/html;charset=UTF-8");
								response.setCharacterEncoding("UTF-8");
								String jsonString = JSONUtil.ToJsongString2(map);
								PrintWriter pw = response.getWriter();
								pw.write(jsonString);
								return null;
							}
					}
					Map reqMap = new HashMap();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ174");
					//接口参数
					reqMap.put("OpenKey",Key);
					BODY b=new BODY();
					b.setGRZH(grzh);
					b.setZJHM(sfzh);
					b.setSJHM(ylsjh);
					b.setCategory("1");
					reqMap.put("BODY",b);
					String result=opk.getOpenKey(reqMap, session);
					logger.info("接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							map.put("code", 0); 
							map.put("msg", null);
							map.put("data",resMap.get("body"));
							list.add(resMap);
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}
					}
					 return "/grwsyyt/gr/grsz/grxx.html";  
				}
}
